<template>
	<view class="login" :style="{height:wh+'px'}">
		<!-- 显示上方的图片 -->
		<view class="title">
			<image src="../../static/images/login.png" mode="aspectFit"></image>
			<image src="../../static/images/login2.png" mode="aspectFit"></image>
		</view>
		<!-- 下方的表单 -->
		<view class="content">
			<uni-forms ref="loginForm" :modelValue="loginFormData" :rules="rules" validateTrigger="blur">
				<uni-forms-item name="username">
					<uni-easyinput v-model="loginFormData.username" placeholder="请输入用户名" />
				</uni-forms-item>
				<uni-forms-item name="password">
					<uni-easyinput v-model="loginFormData.password" placeholder="请输入密码" />
				</uni-forms-item>
			</uni-forms>
			<button class="button" type="primary" @click="login(loginForm)">登录</button>
		</view>
		<!-- 下方的跳转注册 -->
		<view class="register">
			<navigator url="/pages/register/register">还没有账号 ，点击注册</navigator>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue'
	import {
		userStore
	} from '../../store/userStore';
	import {
		CONFIG
	} from '../../utils/config';
	let wh = computed(() => {
		let h = uni.getWindowInfo().windowHeight
		console.log("h=" + h)
		return h
	})
	const loginFormData = ref({
		username: '',
		password: ''
	})
	// 验证表单
	const rules = ref({
		username: {
			rules: [{
					required: true,
					errorMessage: '请输入用户名'
				},
				{
					minLength: 2,
					maxLength: 11,
					errorMessage: '用户必须在{minLength}到{maxLength}个字符之间'
				}, {
					pattern: '^\\w{2,11}$',
					errorMessage: '请输入2-11位数字字母和下划线作为用户名'
				}
			]
		},
		password: {
			rules: [{
				required: true,
				errorMessage: '请输入密码'
			}, {
				pattern: '^[a-zA-Z0-9._!@#$%^&]{3,10}$',
				errorMessage: '请输入3-10位数字字母和特殊字符密码'
			}]
		}
	})
	let loginForm = ref(null)
	// 登录
	let store = userStore()
	
	function login(loginForm) {
				loginForm.validate().then((res) => {
					let params = encodeURI(`{"username":"${res.username}","password":"${res.password}"}`)
					console.log(params)
					uni.$get(`${CONFIG.leancloud_url}/1.1/classes/User?where=${params}`)
						.then(res => {
						 
							if (res.results.length> 0) {
								uni.showToast({
									title: "登录成功",
									icon: "success"
								})
								let user = res.results[0]
								console.log(user)
								store.addUserInfo(user.objectId, user.username, user.phone)
		                        uni.navigateBack()
		
							}
						})
						
		
				}).catch(err => {
					// 验证失败 
					console.log(err)
				})
			}

</script>
<style scoped>
	.login {
		/* 这里添加边框,页面高度就没问题,如果不添加边框,那么页面内容就多余容器高度,显示滚动条,可以滚动 原因待查，。*/
		/* border: 1px solid red; */
		position: relative;
	}

	/* 图片区域距离上方距离 */
	.login .title {
		margin: 100px 30px 0;
		/* border: 1px solid red; */
	}

	/* 设置图片高度,宽度自适应 */
	.login .title image:nth-child(1) {
		height: 50px;

	}

	.login .title image:nth-child(2) {
		height: 30px;
	}

	/* 内容区域的外边距 */
	.login .content {
		margin: 10px 20px;
	}

	/* 注册链接样式 */
	.login .register {
		position: absolute;
		bottom: 20px;
		width: 100vw;
		text-align: center;
		color: #007aff;
		font-size: 20rpx;
	}
</style>